/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.malva.dbpop;

import org.malva.dbpop.utils.LongRandom;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.malva.dbpop.enums.DateType;

/**
 *
 * @author vramirez
 */
public class RandomDate extends AbstractDBPopType {

    private Date fechaini;
    private Date fechafin;
    private long fini;
    private long ffin;
    private LongRandom longrand;
    private DateType type;
    private String format;

    public RandomDate(Calendar fechaini, Calendar fechafin, DateType type, String format) {
        this.fini = fechaini.getTimeInMillis();
        this.ffin = fechafin.getTimeInMillis();
        this.longrand = new LongRandom();
        this.type = type;
    }

    public RandomDate(Calendar fechaini, Calendar fechafin, DateType type) {
        this(fechaini, fechafin, type, null);
    }

    public String next() {
        if (type.equals(DateType.DATE)) {
            return this.nextDate();
        } else if (type.equals(DateType.TIMESTAMP)) {
            return this.nextTimestamp();
        } else if (type.equals(DateType.DATETIME)) {
            return this.nextDateTime();
        } else if (type.equals(DateType.TIME)) {
            return this.nextTime();
        } else {
            return null;
        }
    }

    public String nextDate() {
        if (format != null) {
            return next(format);
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(new Date(fini + longrand.nextLong(ffin - fini + 1)));
    }

    public String nextTimestamp() {
        if (format != null) {
            return next(format);
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd H:mm:ss.S");
        return df.format(new Date(fini + longrand.nextLong(ffin - fini + 1)));
    }

    public String nextDateTime() {
        if (format != null) {
            return next(format);
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd H:mm");
        return df.format(new Date(fini + longrand.nextLong(ffin - fini + 1)));
    }

    public String nextTime() {
        if (format != null) {
            return next(format);
        }
        DateFormat df = new SimpleDateFormat("H:mm:ss");
        return df.format(new Date(fini + longrand.nextLong(ffin - fini + 1)));
    }

    public String next(String format) {
        DateFormat df = new SimpleDateFormat(format);
        return df.format(new Date(fini + longrand.nextLong(ffin - fini + 1)));
    }
}
